﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace StableDiffusionTools.Imaging
{
    public enum GDIResizePresent
    {
        HighSpeed,
        HighQuality,
        LowQuality,
        Default,
    }

    public class BitmapResizeGDISettings : BitmapResizeSettings
    {
        private InterpolationMode interpolationMode;
        public InterpolationMode InterpolationMode
        {
            get { return interpolationMode; }
            set { interpolationMode = value; }
        }

        private PixelOffsetMode pixelOffsetMode;
        public PixelOffsetMode PixelOffsetMode
        {
            get { return pixelOffsetMode; }
            set { pixelOffsetMode = value; }
        }

        private SmoothingMode smoothingMode;
        public SmoothingMode SmoothingMode
        {
            get { return smoothingMode; }
            set { smoothingMode = value; }
        }

        private CompositingQuality compositingQuality;
        public CompositingQuality CompositingQuality
        {
            get { return compositingQuality; }
            set { compositingQuality = value; }
        }

        public BitmapResizeGDISettings(GDIResizePresent present = GDIResizePresent.Default, double scalar = 1.0) : base(scalar)
        {
            //TODO: Tweak presents
            switch (present)
            {
                case GDIResizePresent.HighSpeed:
                    interpolationMode = InterpolationMode.Low;
                    pixelOffsetMode = PixelOffsetMode.HighSpeed;
                    smoothingMode = SmoothingMode.HighSpeed;
                    compositingQuality = CompositingQuality.HighSpeed;
                    break;
                case GDIResizePresent.HighQuality:
                    interpolationMode = InterpolationMode.HighQualityBicubic;
                    pixelOffsetMode = PixelOffsetMode.HighQuality;
                    smoothingMode = SmoothingMode.HighQuality;
                    compositingQuality = CompositingQuality.HighQuality;
                    break;
                case GDIResizePresent.LowQuality:
                    interpolationMode = InterpolationMode.NearestNeighbor;
                    pixelOffsetMode = PixelOffsetMode.HighSpeed;
                    smoothingMode = SmoothingMode.HighSpeed;
                    compositingQuality = CompositingQuality.HighSpeed;
                    break;
                case GDIResizePresent.Default:
                    interpolationMode = InterpolationMode.Default;
                    pixelOffsetMode = PixelOffsetMode.Default;
                    smoothingMode = SmoothingMode.Default;
                    compositingQuality = CompositingQuality.Default;
                    break;
                default:
                    break;
            }
        }

        public BitmapResizeGDISettings(GDIResizePresent present = GDIResizePresent.Default) : this(present, default(Size)) { }
        public BitmapResizeGDISettings(GDIResizePresent present = GDIResizePresent.Default, Size size = default(Size)) : base(size)
        {
            //TODO: Tweak presents
            switch (present)
            {
                case GDIResizePresent.HighSpeed:
                    interpolationMode = InterpolationMode.Low;
                    pixelOffsetMode = PixelOffsetMode.HighSpeed;
                    smoothingMode = SmoothingMode.HighSpeed;
                    compositingQuality = CompositingQuality.HighSpeed;
                    break;
                case GDIResizePresent.HighQuality:
                    interpolationMode = InterpolationMode.HighQualityBicubic;
                    pixelOffsetMode = PixelOffsetMode.HighQuality;
                    smoothingMode = SmoothingMode.HighQuality;
                    compositingQuality = CompositingQuality.HighQuality;
                    break;
                case GDIResizePresent.LowQuality:
                    interpolationMode = InterpolationMode.NearestNeighbor;
                    pixelOffsetMode = PixelOffsetMode.HighSpeed;
                    smoothingMode = SmoothingMode.HighSpeed;
                    compositingQuality = CompositingQuality.HighSpeed;
                    break;
                case GDIResizePresent.Default:
                    interpolationMode = InterpolationMode.Default;
                    pixelOffsetMode = PixelOffsetMode.Default;
                    smoothingMode = SmoothingMode.Default;
                    compositingQuality = CompositingQuality.Default;
                    break;
                default:
                    break;
            }
        }

        public bool SetInterpolationMode(string value)
        {
            return Enum.TryParse(value, out interpolationMode);
        }

        public bool SetPixelOffset(string value)
        {
            return Enum.TryParse(value, out pixelOffsetMode);
        }

        public bool SetSmoothingMode(string value)
        {
            return Enum.TryParse(value, out smoothingMode);
        }

        public bool SetCompositingQuality(string value)
        {
            return Enum.TryParse(value, out compositingQuality);
        }
    }
}
